/**
 * 以puppter的方式直接解析并运行json
 * @author mumulin
 * @email xinglinhe@qq.com
 * @date 2025/11
 */
import readlineSync from 'readline-sync'
import base,{
	jump,action_click,action_delay,
	action_jumpIndex,action_reStep,action_replayCheck,
	action_pageClick,action_replace,action_fileUpload
} from '../../lib/base.js'
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url); // 当前文件的绝对路径
const __dirname = dirname(__filename); // 当前文件所在目录的绝对路径

async function acceptCookies(page){
	return
	try{
		await page.waitForSelector("#CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll",{
			timeout: 500
		})
		await page.click("#CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll")
	}catch(e){}
}

class dataWork {
    constructor() {}
	async signIn({data, step, index, page}){
		const option = [
			{runType : action_replace, target : "aria/Email Address",type:"change", replace: {value:data.account.Email}},
			{runType : action_replace, target : "aria/Password",type:"change", replace: { value: data.account.Password }},
			{runType : action_replace, target : "aria/Verification code",type:"change", replace: { value: ()=>readlineSync.question('Verification code: ').trim() }},
			{runType : action_delay, target : "#continue",type:"click", delay: 3000 },
			{runType : action_delay, target : "aria/Toggle navigation",type:"click", delay: 2000 },
			{runType : action_replayCheck, target : "aria/Edit application",type:"click", replayCheck: ()=>base.checkHasDom(page, "body > div.container > div.row li:nth-of-type(1) > a") },
		]
		return await base.eventTrusteeshipAll({data, step, index, page, option})
	}
	async newApplication({data, step, index, page}){
		const option = [
			{runType : action_jumpIndex, jumpIndex : 1},
			{runType : action_replace, target : "aria/Search by course, study pattern or study location",type:"change", replace: {value: "global public policy"} },
			{runType : action_delay, target : "aria/Create Application",type:"click", delay: 3000 },
		]
		return await base.eventTrusteeshipAll({data, step, index, page, option})
	}
	async PersonalDetails({data, step, index, page}){
		const option = [
			{runType : action_jumpIndex, jumpIndex : 1},
			{runType : action_delay, target : "aria/Application form overview",type:"click", delay: 3000 },
			{runType : action_replace, target : "aria/Mrs",type:"click", replace: {
				selectors: [ [ "aria/Mrs" ] ]
			} },
			{runType : action_replace, target : "aria/Date of Birth *",type:"change", replace: {"value": "22/12/1995"} },
			{runType : action_replace, target : "aria/Country of birth",type:"click", replace: {
				selectors: [ [ "aria/China" ] ]
			} },
			{runType : action_replace, target : "aria/Nationality",type:"click", replace: {
				selectors: [ [ "aria/China" ] ]
			} },
			{runType : action_replace, target : "aria/Country of permanent residence",type:"click", replace: {
				selectors: [ [ "aria/Brunei Darussalam" ] ]
			} },
		]
		return await base.eventTrusteeshipAll({data, step, index, page, option})
	}
	async ContactDetails({data, step, index, page}){
		const option = [
			{runType : action_jumpIndex, jumpIndex : 1},
			{runType : action_delay, target : "li:nth-of-type(2) span.link-text",type:"click", delay: 2000 },
			{runType : action_delay, target : "aria/Application form overview",type:"click", delay: 3000 },
			{runType : action_replace, target : "aria/Address line 1*",type:"change", replace: {"value": "9F123"} },
			{runType : action_replace, target : "aria/Address line 2",type:"change", replace: {"value": "Dongfeng West Road123"} },
			{runType : action_replace, target : "aria/City*",type:"change", replace: {"value": "Guangzhou"} },
			{runType : action_replace, target :  "aria/Post code / ZIP code*",type:"change", replace: {"value": "510642"} },
			{runType : action_replace, target : "aria/Alderney",type:"click", replace: {
				selectors: [ [ "aria/Alderney" ] ]
			} },
			{runType : action_replace, target :  "aria/Mobile number*",type:"change", replace: {"value": "+86 13411111111"} },

		]
		return await base.eventTrusteeshipAll({data, step, index, page, option})
	}
	async AcademicHistory({data, step, index, page}){
		const option = [
			{runType : action_delay, target : "li:nth-of-type(3) span.link-text",type:"click", delay: 3000 },
		]
		return await base.eventTrusteeshipAll({data, step, index, page, option})
	}
	async AcademicHistoryAdd({data, step, index, page}){
		const option = [
			{runType : action_jumpIndex, jumpIndex : 1},
			{runType : action_delay, target : "#select2-uob_institutionid-container > span",type:"click", delay: 3000 },
			{runType : action_replace, target : "aria/Albania",type:"click", replace: {
				selectors: [ [ "aria/Albania" ] ]
			} },
			{runType : action_replace, target : "aria/Other",type:"click", replace: {
				selectors: [ [ "aria/Other" ] ]
			} },
			{runType : action_replace, target :  "aria/Other education institution *",type:"change", replace: {"value": "Shenzhen Technology University"} },
			{runType : action_replace, target : "aria/Degree qualification type",type:"click", replace: {
				selectors: [ [ "aria/Other" ] ]
			} },
			{runType : action_replace, target :  "#uob_otherqualificationtype",type:"change", replace: {"value": "Bachelor of xxx"} },
			{runType : action_replace, target : "aria/Academic subject",type:"click", replace: {
				selectors: [ [ "aria/Other" ] ]
			} },
			{runType : action_replace, target :  "#uob_otheracademicsubject",type:"change", replace: {"value": "Accounting and Finance"} },
			{runType : action_replace, target :  "aria/When did you start studying for this degree / qualification? *",type:"change", replace: {"value": "01/09/2022"} },
			{runType : action_replace, target :  "aria/When did you complete this degree / qualification? *",type:"change", replace: {"value": "01/07/2026"} },
			{runType : action_replace, target : "#fulltime_option",type:"click", replace: {
				selectors: [ [ "#fulltime_option" ] ] //选项2 #parttime_option
			} },
			{runType : action_replace, target : "aria/aria/How were your degree",type:"click", replace: {
				selectors: [ [ "aria/Other" ] ]
			} },
			{runType : action_replace, target :  "aria/Other result",type:"change", replace: {"value": "90.78"} },
		]
		return await base.eventTrusteeshipAll({data, step, index, page, option})
	}
	async AcademicHistoryUpload({data, step, index, page}){
		const option = [
			{runType : action_jumpIndex, jumpIndex : 1},
			{runType : action_fileUpload, target : "#AttachFile",type:"click", files: `${__dirname}/pdf.pdf` },
		]
		return await base.eventTrusteeshipAll({data, step, index, page, option})
	}
	async EnglishLanguage({data, step, index, page}){
		const option = [
			{runType : action_jumpIndex, jumpIndex : 1},
			{runType : action_delay, target : "li:nth-of-type(4) span.link-text",type:"click", delay: 2000 },
			{runType : action_replace, target : "#uob_englishfirstlanguage_1",type:"click", replace: {
				selectors: [ [ "#uob_englishfirstlanguage_1" ] ]
			} },
			{runType : action_replace, target : "#uob_englishqualificationobtained_0",type:"click", replace: {
				selectors: [ [ "#uob_englishqualificationobtained_0" ] ]
			} },
		]
		return await base.eventTrusteeshipAll({data, step, index, page, option})
	}
	async EnglishLanguageAdd({data, step, index, page}){
		const option = [
			{runType : action_jumpIndex, jumpIndex : 1},
			{runType : action_delay, target : "aria/Add certificate details",type:"click", delay: 1000 },
			{runType : action_replace, target : "aria/IELTS (International English Language Testing System)",type:"click", replace: {
				selectors: [ [ "aria/IELTS (International English Language Testing System)" ] ]
			} },
			{runType : action_replace, target :  "aria/Registration number *",type:"change", replace: {"value": "111111111111111"} },
			{runType : action_replace, target :  "aria/Date of test",type:"change", replace: {"value": "01/10/2025"} },
			{runType : action_replace, target : "aria/Listening score",type:"click", replace: {
				selectors: [ [ "aria/9.0" ] ]
			} },
			{runType : action_replace, target : "aria/Writing score",type:"click", replace: {
				selectors: [ [ "aria/9.0" ] ]
			} },
			{runType : action_replace, target : "aria/Reading score",type:"click", replace: {
				selectors: [ [ "aria/9.0" ] ]
			} },
			{runType : action_replace, target : "aria/Speaking score",type:"click", replace: {
				selectors: [ [ "aria/9.0" ] ]
			} },
			{runType : action_replace, target : "aria/Overall score",type:"click", replace: {
				selectors: [ [ "aria/9.0" ] ]
			} },
		]
		return await base.eventTrusteeshipAll({data, step, index, page, option})
	}
	async EnglishLanguageAddOther({data, step, index, page}){
		const option = [
			{runType : action_jumpIndex, jumpIndex : 1},
			{runType : action_delay, target : "aria/Add certificate details",type:"click", delay: 1000 },
			{runType : action_replace, target :   "aria/Certificate details",type:"change", replace: {"value": "LanguageCert"} },
			{runType : action_replace, target :  "aria/Date of test",type:"change", replace: {"value": "01/10/2025"} },
			{runType : action_replace, target :  "aria/Result",type:"change", replace: {"value": "111111111"} },
		]
		return await base.eventTrusteeshipAll({data, step, index, page, option})
	}
	async EnglishLanguageUpload({data, step, index, page}){
		const option = [
			{runType : action_jumpIndex, jumpIndex : 1},
			{runType : action_fileUpload, target : "#AttachFile",type:"click", files: `${__dirname}/pdf.pdf` },
		]
		return await base.eventTrusteeshipAll({data, step, index, page, option})
	}
	async WorkExperience({data, step, index, page}){
		const option = [
			{runType : action_jumpIndex, jumpIndex : 1},
			{runType : action_delay, target : "li:nth-of-type(5) span.link-text",type:"click", delay: 2000 },
			{runType : action_replace, target : "#uob_workexperience_0",type:"click", replace: {
				selectors: [ [ "#uob_workexperience_0" ] ]
			} },
			{runType : action_replace, target : "#uob_professionalqualifications_0",type:"click", replace: {
				selectors: [ [ "#uob_professionalqualifications_0" ] ]
			} },

		]
		return await base.eventTrusteeshipAll({data, step, index, page, option})
	}
	async WorkExperienceAdd({data, step, index, page}){
		const option = [
			{runType : action_jumpIndex, jumpIndex : 1},
			{runType : action_delay, target : "aria/Add work experience",type:"click", delay: 1000 },
			{runType : action_replace, target : "aria/Job title",type:"change", replace: {"value": "Staff"} },
			{runType : action_replace, target : "aria/Employer",type:"change", replace: {"value": "HSBC"} },
			{runType : action_replace, target : "aria/From date *",type:"change", replace: {"value": "01/09/2025"} },
			{runType : action_replace, target : "aria/To date",type:"change", replace: {"value": "01/10/2025"} },
			{runType : action_replace, target : "#uob_employmentmode_0",type:"click", replace: {
				selectors: [ [ "#uob_employmentmode_0" ] ]
			} },
		]
		return await base.eventTrusteeshipAll({data, step, index, page, option})
	}
	async WorkExperienceQualifcation({data, step, index, page}){
		const option = [
			{runType : action_jumpIndex, jumpIndex : 1},
			{runType : action_delay, target : "aria/Add qualification",type:"click", delay: 1000 },
			{runType : action_delay, target : "#uob_otherqualificationtype",type:"click", delay: 1000 },
			{runType : action_delay, target : "#uob_otherinstitution",type:"click", delay: 1000 },
			{runType : action_replace, target : "aria/Other",type:"click", replace: {
				selectors: [ [ "aria/Other" ] ]
			} },
			{runType : action_replace, target : "#uob_otherqualificationtype",type:"change", replace: {"value": "abcabc"} },
			{runType : action_replace, target : "aria/Awarding body",type:"click", replace: {
				selectors: [ [ "aria/Other" ] ]
			} },
			{runType : action_replace, target : "aria/Date or expected date of award *",type:"change", replace: {"value": "01/10/2025"} },
		]
		return await base.eventTrusteeshipAll({data, step, index, page, option})
	}
	async PersonalStatement({data, step, index, page}){
		const option = [
			{runType : action_jumpIndex, jumpIndex : 1},
			{runType : action_delay, target : "li:nth-of-type(6) span.link-text",type:"click", delay: 2000 },
			{runType : action_replace, target : "#uob_uploadpersonalstatement_0",type:"click", replace: {
				selectors: [ [ "#uob_uploadpersonalstatement_0" ] ]
			} },
			{runType : action_delay, target : "#uob_personalstatement",type:"click", delay: 1000 },
			{runType : action_replayCheck, target : "#uob_personalstatement",type:"click", replayCheck: ()=>false },
			{runType : action_replayCheck, target : "#uob_personalstatement",type:"change", replayCheck: ()=>false },
			{runType : action_replace, target : "#uob_personalstatement",type:"change", replace: {"value": "sadfghjklkjmzxnbvcoeiqryasdasd"} },

		]
		return await base.eventTrusteeshipAll({data, step, index, page, option})
	}
	async PersonalStatementUpload({data, step, index, page}){
		const option = [
			{runType : action_jumpIndex, jumpIndex : 1},
			{runType : action_delay, target : "aria/Upload personal statement",type:"click", delay: 1000 },
			{runType : action_fileUpload, target : "#AttachFile",type:"click", files: `${__dirname}/pdf.pdf` },
		]
		return await base.eventTrusteeshipAll({data, step, index, page, option})
	}
	async References({data, step, index, page}){
		const option = [
			{runType : action_jumpIndex, jumpIndex : 1},
			{runType : action_delay, target : "li:nth-of-type(7) span.link-text",type:"click", delay: 2000 },
		]
		return await base.eventTrusteeshipAll({data, step, index, page, option})
	}
	async ReferencesAdd({data, step, index, page}){
		const option = [
			{runType : action_jumpIndex, jumpIndex : 1},
			{runType : action_replace, target : "#uob_referencetype_0",type:"click", replace: {
				selectors: [ [ "#uob_referencetype_0" ] ]
			} },
			{runType : action_replace, target : "aria/Miss",type:"click", replace: {
				selectors: [ [ "aria/Mrs" ] ]
			} },
			{runType : action_replace, target : "aria/First name",type:"change", replace: {value: "abc"} },
			{runType : action_replace, target : "aria/Last name",type:"change", replace: {value: "def"} },
			{runType : action_replace, target : "aria/Position or role",type:"change", replace: {value: "Professor"} },
			{runType : action_replace, target :  "aria/Institution or company",type:"change", replace: {value: "Jinan University"} },
			{runType : action_replace, target :  "aria/Referee email address",type:"change", replace: {value: "nijiliang@eiceducation.com.cn"} },
			{runType : action_replace, target : "#uob_uploadreference_0",type:"click", replace: {
				selectors: [ [ "#uob_uploadreference_0" ] ]
			} },
			{runType : action_delay, target : "#AttachFile",type:"click", delay: 2000 },
			{runType : action_replayCheck, target :"#AttachFile",type:"click", replayCheck: ()=>true },
			{runType : action_fileUpload, target : "#AttachFile",type:"click", files: `${__dirname}/pdf.pdf` },
		]
		return await base.eventTrusteeshipAll({data, step, index, page, option})
	}
	async Funding({data, step, index, page}){
		const option = [
			{runType : action_jumpIndex, jumpIndex : 1},
			{runType : action_delay, target : "li:nth-of-type(8) span.link-text",type:"click", delay: 2000 },
			{runType : action_replace, target : "#uob_fundingsource_1",type:"click", replace: {
				selectors: [ [ "#uob_fundingsource_1" ] ]
			} },
			{runType : action_replayCheck, target :"#uob_securedsponsorship_0",type:"click", replayCheck: ()=>true },
			{runType : action_replace, target : "#uob_securedsponsorship_0",type:"click", replace: {
				selectors: [ [ "#uob_securedsponsorship_0" ] ]
			} },
			{runType : action_replayCheck, target :"aria/State Oil Company Of Azerbaijan Republic (SOCAR), Azerbaijan",type:"click", replayCheck: ()=>true },
			{runType : action_replace, target : "aria/State Oil Company Of Azerbaijan Republic (SOCAR), Azerbaijan",type:"click", replace: {
				selectors: [ [ "aria/State Oil Company Of Azerbaijan Republic (SOCAR), Azerbaijan" ] ]
			} },
		]
		return await base.eventTrusteeshipAll({data, step, index, page, option})
	}
	async Equality({data, step, index, page}){
		const option = [
			{runType : action_jumpIndex, jumpIndex : 1},
			{runType : action_delay, target : "li.edi-last-item span.link-text",type:"click", delay: 2000 },
			{runType : action_replace, target : "text/No disability",type:"click", replace: {
				selectors: [ [ "text/No disability" ] ]
			} },
			{runType : action_replace, target : "aria/Arab",type:"click", replace: {
				selectors: [ [ "aria/Arab" ] ]
			} },
			{runType : action_replace, target : "aria/No Religion",type:"click", replace: {
				selectors: [ [ "aria/No Religion" ] ]
			} },
			{runType : action_replace, target : "#uob_legalsex_0",type:"click", replace: {
				selectors: [ [ "#uob_legalsex_0" ] ]
			} },
			{runType : action_replace, target : "aria/Man",type:"click", replace: {
				selectors: [ [ "aria/Man" ] ]
			} },
			{runType : action_replace, target : "#uob_genderidentityasatbirth_0",type:"click", replace: {
				selectors: [ [ "#uob_genderidentityasatbirth_0" ] ]
			} },
			{runType : action_replace, target : "aria/Bisexual",type:"click", replace: {
				selectors: [ [ "aria/Bisexual" ] ]
			} },
			{runType : action_replace, target : "aria/Young people/children",type:"click", replace: {
				selectors: [ [ "aria/Young people/children" ] ]
			} },
			{runType : action_replace, target : "aria/No",type:"click", replace: {
				selectors: [ [ "aria/No" ] ]
			} },
		]
		return await base.eventTrusteeshipAll({data, step, index, page, option})
	}
}

export default {
	// 基础运行分发
	async run(params) {
		const work =  new dataWork();
		const stepsName = params.stepsName?.trim()||null
		if(!stepsName) return
		await acceptCookies(params.page)
		if(work[stepsName]) return await work[stepsName](params);
	}
}